<template>
  <div>
    <el-tree
      node-key="id"
      ref="tree"
      :data="list"
      :props="{ label: 'name', children: 'children' }"
      default-expand-all
      show-checkbox
      check-strictly
    ></el-tree>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="$emit('close')">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from "@/api/permisson";
import { tranDataToTree } from "@/utils";
import { getRolesDetail, assignPerm } from "@/api/setting";
export default {
  props: {
    roleId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.loadPermissionList();
  },
  methods: {
    // 渲染列表
    async loadPermissionList() {
      try {
        const res = await getPermissionList();
        console.log("getPermissionList", res);
        // 保存数据 渲染
        this.list = tranDataToTree(res.data);

        // 数据回填
        const info = await getRolesDetail(this.roleId);
        console.log("getRolesDetail", info);
        this.$refs.tree.setCheckedKeys(info.data.permIds);
      } catch (err) {
        console.log("getPermissionList", err);
      }
    },
    // 保存角色的权限
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({id:this.roleId,permIds:permIds});
        console.log("assignPerm", res);
        this.$message.success(res.message)
        this.$emit('close')
        // 保存数据
      } catch (err) {
        console.log("assignPerm", err);
      }
    },
  },
};
</script>

<style>
</style>